﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vega.USiteBuilder;
using umbraco.NodeFactory;
using SPCFMedicineInformer.DocumentTypes;
using SPCFMedicineInformer.Constants;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using SPCFMedicineInformer.Helpers;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_Frontpage : System.Web.UI.MasterPage
    {
        int Id;
        Node umbracoNode = null;
        protected void Page_Init(object sender, EventArgs e)
        {
            ((umbraco.UmbracoDefault)this.Page).ValidateRequest = false;

        }
        protected void Page_Load(object sender, EventArgs e)
        {

            umbracoNode = Helpers.UmbracoNodeHelper.GetWebsitePage(SearchType.Name, "Home");
            if (!IsPostBack)
            {
                if (umbracoNode != null)
                {
                    Id = umbracoNode.Id;
                    var node = ContentHelper.GetByNodeId<FrontPage>(Id);
                    taLeftContent.Value = node.LeftContent;
                    taRightContent.Value = node.RightContent;
                    taFooterText.Value = node.FooterText;
                    txtTitle.Text = node.MenuTitle;

                }
            }
        }
        void UpdateNode()
        {
            if (umbracoNode != null)
            {
                var doc = new Document(umbracoNode.Id);
                TransferDetailsToUmbraco(doc);
            } 
        }
        void TransferDetailsToUmbraco(Document doc)
        {
            doc.getProperty(UmbracoPropertyName.MENU_TITLE).Value = txtTitle.Text;
            doc.getProperty(UmbracoPropertyName.LEFTCONTENT).Value = taLeftContent.Value;
            doc.getProperty(UmbracoPropertyName.RIGHTCONTENT).Value = taRightContent.Value;
            doc.getProperty(UmbracoPropertyName.FOOTER_TEXT).Value = taFooterText.Value;
            doc.Publish(User.GetUser(0));
            umbraco.library.UpdateDocumentCache(doc.Id);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            UpdateNode();
            FormHelper.ShowMessage(MessageType.Done, "Frontpage Succesfully Saved!", MsgLiteral);
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/site-management/");
        }


        
    }
}